package com.project.excel;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.graphics.Color;
import android.widget.TextView;

public class CellView extends TextView implements DataType {

	private int cellId;
	private int height;
	private int width;
	private int color;
	private int clicked;
	private Object cellData;
	private boolean isReferred;
	private ArrayList<CellView> parentCells;
	private ArrayList<CellView> children;

	public CellView(Context context, int id, int height, int width) {
		super(context);
		// TODO Auto-generated constructor stub
		this.cellId = 28 * id;
		this.height = height;
		this.width = width;
		this.color = Color.BLACK;
		this.setId(this.cellId);
		this.setHeight(this.height);
		this.setWidth(this.width);
		this.setBackgroundResource(R.drawable.back);
		this.setTextColor(this.color);
		this.isReferred = false;
		this.parentCells = new ArrayList<CellView>();
		this.children = new ArrayList<CellView>();
	}

	public ArrayList<CellView> getChildren() {
		return children;
	}

	public void addChildren(CellView children) {
		this.children.add(children);
	}

	public ArrayList<CellView> getParentCells() {
		return parentCells;
	}

	public void addParentCells(CellView cell) {
		this.parentCells.add(cell);
	}

	public boolean isReferred() {
		return isReferred;
	}

	public void setReferred(boolean isReferred) {
		this.isReferred = isReferred;
	}
	public int getClicked() {
		return clicked;
	}

	public void setClicked(int clicked) {
		this.clicked = clicked;
	}
	
	public int getCellId() {
		return cellId;
	}

	public void setCellId(int cellId) {
		this.cellId = cellId;
	}
	
	public void setCellText(int id) {
		this.setText(""+id+"");		
	}
	
	public void setCellText(String character){
		this.setText(" "+character);
	}

	@Override
	public void setOnTouchListener(OnTouchListener l) {
		// TODO Auto-generated method stub
		super.setOnTouchListener(l);
	}
	
	public void setCellData(Object data){
		if (data instanceof Integer){
			this.cellData = (Integer) data;
		}
		else if (data instanceof String){
			this.cellData = (String) data;
		}
	}
	
	public Object getCellData(){
		return this.cellData;
	}
	
	public int getType(Object data){
		if (isInt(data)){
			return 1;	//if data is an int type
		}
		else{
			return 0;	//else is a string : valid or invalid
		}
	}
	
	public boolean isString(Object data){
		return (data.getClass()==String.class)?(true):false; // if the type is string
	}
	
	 public boolean isInt(Object data){  
		 String number = data.toString();
         boolean isValid = false;  
         /*Explaination: 
            [-+]?: Can have an optional - or + sign at the beginning. 
            [0-9]*: Can have any numbers of digits between 0 and 9 
            \\.? : the digits may have an optional decimal point. 
        [0-9]+$: The string must have a digit at the end. 
        If you want to consider x. as a valid number change 
            the expression as follows. (but I treat this as an invalid number.). 
           String expression = "[-+]?[0-9]*\\.?[0-9\\.]+$"; 
           */  
           String expression = "[-+]?[0-9]*\\.?[0-9]+$";  
           CharSequence inputStr = number;  
           Pattern pattern = Pattern.compile(expression);  
           Matcher matcher = pattern.matcher(inputStr);  
           if(matcher.matches()){  
              isValid = true;  
           }  
           return isValid;  
         }  
	

}
